package org.vectrics.user.web.struts;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Category;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.struts.PageListTag;
import org.vectrics.user.Person;
import org.vectrics.user.PersonSearchCriteria;
import org.vectrics.user.PersonSearcher;

public abstract class UserGroupManagerTag  extends PageListTag {

	private static final long serialVersionUID = -2116365815257265827L;
	
	private static Category log = Category.getInstance(UserGroupManagerTag.class);
    public String PAGE_KEY_PARAM = "page_key";

    public UserGroupManagerTag() {
    	super();
    }
    
    protected abstract PersonSearchCriteria getUserCriteria();
    protected abstract PersonSearcher getPersonSearcher();
    protected abstract void makeHeaderCells(Row row);
    protected abstract void makeObjectRow(Row row, Person user);
    protected abstract void setTableProperties(BorderTable table);
    //protected abstract Collection getUserPrivileges();

	protected void init() {
	}
	
	public void setPageSize(int size) {
		super.setPageSize(size);
	}

    protected abstract String getPageListLink();
    protected abstract boolean showNewUserLink();
    
    protected final PageList createPageList() {
    	log.debug("createPageList()");
    	PersonPageList pageList = new PersonPageList(this.getPersonSearcher());
    	PersonSearchCriteria criteria = getUserCriteria();
    	criteria.addDomain(ThreadProperties.getDomain());
    	if (log.isDebugEnabled()) {
    		log.debug("UserSelectCriteria = " + criteria);
    	}
    	pageList.setCriteria(criteria);
    	return(pageList);
    }
    
    protected void showList(String description, 
    		Cell mainCell, List userList, Form form)   {
    	if (log.isDebugEnabled()) {
    		log.debug("UserGroupManagerTag - instance class = " + this.getClass().getName());
    		log.debug("showList() called. list size = " + userList.size());
    	}
        
        Iterator userIter = userList.iterator();
        BorderTable table = new BorderTable();

        table.setID("border_table");
        table.setWidth("100%");
        mainCell.addElement(table);
        
        if (userIter.hasNext()) {
			log.debug("showList() - list has elements, displaying.");
            Row row = table.addRow();
            makeHeaderCells(row);
            while (userIter.hasNext()) {
				log.debug("showList() - displaying next user...");
				Person person = (Person)userIter.next();
				if (log.isDebugEnabled()) {
					log.debug("showList() - userId = " + person.getId());
					log.debug("showList() - user = " + person.getName());
				}
                row = table.addRow();
				log.debug("showList() - calling abstract method: makeObjectRow()");
				if (person != null)
					makeObjectRow(row, person);
            }
        } else {
            log.debug("doRender() - no users.");
            Row row = table.addRow();
        }
    }

    protected void fillBottomCell(Cell cell) {
    }
}